package com.richest.api.utils;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.richest.api.dao.SysConfigMapper;
import com.richest.bean.SysConfigBean;

/**
 * 
 * @author zhaohongchao 2019年3月26日 上午9:20:47
 * @email 70375739@qq.com
 */
@Component
public class AliAppInfo {
	@Autowired
	private SysConfigMapper sysConfigMapper;

	private final static String key = "alipay";
	private final static String pay_key = "PAY_CONFIG_KEY";
	private static JSONObject param_value = null;
	private static SysConfigMapper mapper;

	@PostConstruct
	public void init() {
		mapper = this.sysConfigMapper;
	}

	private static void initParamsValue() {
		Map<String, Object> params = new HashMap<>();
		params.put("key", key);
		params.put("pay_key", pay_key);
		SysConfigBean sysConfigBean = mapper.queryByPayKey(params);
		if (null != sysConfigBean) {
			param_value = JSONObject.parseObject(sysConfigBean.getParam_value());
		}
	}

	// 1.商户appid
	public static String getAppid() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("app_id");
		}
		return "";
	}

	// 2.私钥 pkcs8格式的
	public static String getRsaPrivateKey() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("rsa_private_key");
		}
		return "";
	}

	// 3.支付宝公钥
	public static String getAliPayPublicKey() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("alipay_public_key");
		}
		return "";
	}

	// 4.服务器异步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	public static String getNotifyUrl() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("notify_url");
		}
		return "";
	}

	// 5.页面跳转同步通知页面路径 需http://或者https://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
	// 商户可以自定义同步跳转地址
	public static String getReturnUrl() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("return_url");
		}
		return "";
	}

	// 6.请求网关地址
	public static String getPayUrl() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("pay_url");
		}
		return "";
	}

	// 7.编码
	public static String getCharSet() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("char_set");
		}
		return "";
	}

	// 8.返回格式
	public static String getFormat() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("format");
		}
		return "";
	}

	// 9.加密类型
	public static String getSignType() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("sign_type");
		}
		return "";
	}

	// 10.商家id
	public static String getSellerId() {
		if (null == param_value) {
			initParamsValue();
		}
		if (null != param_value && param_value.size() > 0) {
			return param_value.getString("seller_id");
		}
		return "";
	}
}
